<template>
  <markdown-body id="markdown-body">
    <Viewer :value="value" :plugins="plugins" />
  </markdown-body>
</template>

<script setup lang="ts">
import gfm from "@bytemd/plugin-gfm";
import highlight from "@bytemd/plugin-highlight";
import { Editor, Viewer } from "@bytemd/vue-next";
import { ref, withDefaults, defineProps } from "vue";
import "highlight.js/styles/github.css"; // 引入代码高亮
/**
 *  定义组件属性类型
 */
interface Props {
  value: string;
}

// 给组件指定初始值
const props = withDefaults(defineProps<Props>(), {
  value: () => "",
});

const plugins = [
  gfm(),
  highlight(),
  // Add more plugins here
];
</script>

<style>
/* 隐藏组件来源图标 */
.bytemd-toolbar-icon.bytemd-tippy.bytemd-tippy-right:last-child {
  display: none;
}

/* 引入主题 */
/*@import "../assets/css/channing-cyan.scss";*/
/* ByteMD主题 channing-cyan */
.markdown-body {
  word-break: break-word;
  line-height: 1.75;
  font-weight: 400;
  font-size: 15px;
  overflow-x: hidden;
  color: #2b2b2b;
  font-family: -apple-system, system-ui, BlinkMacSystemFont, Helvetica Neue,
    PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
  background-image: linear-gradient(
      90deg,
      rgba(159, 219, 252, 0.15) 3%,
      rgba(0, 0, 0, 0) 3%
    ),
    linear-gradient(360deg, rgba(159, 219, 252, 0.15) 3%, rgba(0, 0, 0, 0) 3%);
  background-size: 20px 20px;
  background-position: center center;

  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    padding: 30px 0;
    margin-top: 35px;
    margin-bottom: 10px;
    color: $primaryColor;
  }

  h1 {
    font-size: 30px;
    text-align: center;
    position: relative;
    width: max-content;
    margin: 0 auto;

    &:before {
      position: absolute;
      content: "";
      z-index: -1;
      top: -20px;
      height: 100%;
      width: 100px;
      left: 0;
      right: 0;
      margin: 0 auto;
      background: url()
        no-repeat center center;
      background-size: 64px 64px;
      opacity: 0.84;
    }

    &:after {
      position: absolute;
      content: "";
      width: 150%;
      left: -25%;
      height: 50%;
      bottom: 12px;
      border-radius: 50%;
      background: linear-gradient(
        rgba(0, 0, 0, 0) 80%,
        rgba(77, 208, 225, 0.8)
      );
      background-size: 400% 200%;
      opacity: 0.6;
      animation: h1Animate 6s infinite linear;
    }

    @keyframes h1Animate {
      0% {
        background-position: 100% 100%;
      }
      50% {
        background-position: 100% 50%;
      }
      100% {
        background-position: 100% 100%;
      }
    }
  }

  h2 {
    display: block;
    border-bottom: 4px solid $primaryColor;
    position: relative;
    font-size: 24px;
    padding: 12px 32px;
    margin: 30px 0;

    &::before {
      content: "";
      display: block;
      width: 24px;
      height: 24px;
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      margin: auto;
      background-size: 24px 24px;
      background-image: url();
    }

    &:after {
      content: "";
      display: block;
      position: absolute;
      right: 0;
      bottom: 0;
      width: 400px;
      height: 10px;
      border-top-right-radius: 24px;
      background: linear-gradient(to right, #fff, $primaryColor);
      max-width: 50vw;
    }
  }

  h3 {
    margin: 30px 0;
    font-size: 18px;
    position: relative;
    padding: 4px 32px;
    width: max-content;
    &:before {
      border-bottom: 2px solid $primaryColor;
      width: 100%;
      content: "";
      display: block;
      //width: 28px;
      height: 28px;
      position: absolute;
      left: 0;
      top: 0;
      bottom: -2px;
      margin: auto;
      background-size: 28px 28px;
      background-image: url();
      background-repeat: no-repeat;
      animation: h3AnimationBefore 2s infinite alternate;
    }

    @keyframes h3AnimationBefore {
      0% {
        width: 28px;
      }
      25% {
        width: 100%;
      }
      50% {
        width: 100%;
      }
      100% {
        width: 100%;
      }
    }

    &:after {
      content: "";
      display: block;
      width: 28px;
      height: 28px;
      position: absolute;
      border: 2px solid $primaryColor;
      border-radius: 50%;
      right: -15px;
      top: 0;
      bottom: 0;
      margin: auto;
      background-size: 28px 28px;
      background-image: url();
      animation: h3AnimationAfter 2s infinite alternate;
    }

    @keyframes h3AnimationAfter {
      0% {
        transform: rotate3d(0, 0, 1, 0);
      }
      10% {
        transform: rotate3d(0, 0, 1, 0);
      }
      50% {
        transform: rotate3d(0, 0, 1, -360deg);
      }
      100% {
        transform: rotate3d(0, 0, 1, -360deg);
      }
    }
  }

  h4 {
    font-size: 16px;
  }

  h5 {
    font-size: 15px;
  }

  h6 {
    margin-top: 5px;
  }

  p {
    line-height: inherit;
    margin: $line-space 0;
    letter-spacing: 2px;
    font-size: 14px;
    word-spacing: 2px;
  }

  img {
    max-width: 80%;
    border-radius: 6px;
    display: block;
    margin: 20px auto !important;
    object-fit: contain;
    box-shadow: 0 0 16px rgba(110, 110, 110, 0.45);
  }

  /* 图片描述文字 */
  figcaption {
    display: block;
    font-size: 13px;
    color: #2b2b2b;

    &:before {
      content: "";
      background-image: url();
      display: inline-block;
      width: 18px;
      height: 18px;
      background-size: 18px;
      background-repeat: no-repeat;
      background-position: center;
      margin-right: 5px;
      margin-bottom: -5px;
    }
  }

  /* 分隔线 */
  hr {
    border-top: 1px solid $primaryColor;
    border-bottom: none;
    border-left: none;
    border-right: none;
    margin-top: 32px;
    margin-bottom: 32px;
  }

  /* 删除线 */
  del {
    color: $primaryColor;
  }

  code {
    word-break: break-word;
    border-radius: 2px;
    overflow-x: auto;
    background-color: $primaryLight3;
    color: $primaryDark1;
    //font-size: 0.9em;
    padding: 0.195em 0.4em;
  }

  pre {
    font-family: $monospace-font;
    overflow: auto;
    position: relative;
    line-height: 1.75;
    box-shadow: 0 0 8px rgba(110, 110, 110, 0.45);
    border-radius: 4px;
    margin: 16px;

    &:before {
      content: "";
      display: block;
      height: 30px;
      width: 100%;
      margin-bottom: -7px;
      background: url()
        10px 10px no-repeat;
      background-size: 40px;
    }

    > code {
      font-size: 12px;
      padding: 15px 12px;
      margin: 0;
      word-break: normal;
      display: block;
      overflow-x: auto;
      color: #333;
      background: #f8f8f8;
    }
  }

  a {
    color: $primaryColor;
    border-bottom: 1px solid $primaryColor;
    font-weight: 400;
    text-decoration: none;
    margin: 0 4px;

    &:hover,
    &:active {
      background-color: $primaryLight1;
    }
  }

  strong {
    color: $primaryDark1;

    &::before {
      content: "「";
    }

    &::after {
      content: "」";
    }
  }

  em {
    font-style: normal;
    color: $primaryColor;
    font-weight: bold;
  }

  table {
    display: inline-block !important;
    font-size: 12px;
    width: auto;
    max-width: 100%;
    overflow: auto;
    border: solid 1px #f6f6f6;
  }

  thead {
    background: #f6f6f6;
    color: #000;
    text-align: left;
  }

  tr:nth-child(2n) {
    background-color: $primaryLight2;
  }

  th,
  td {
    padding: 12px 7px;
    line-height: 24px;
  }

  td {
    min-width: 120px;
  }

  /* 引用 */
  blockquote {
    margin: 2em 0;
    padding: 24px 32px;
    border-left: 4px solid $primaryDark1;
    background: RGBA(77, 208, 225, 0.15);
    position: relative;

    &::before {
      content: "❝";
      top: 8px;
      left: 8px;
      color: $primaryColor;
      font-size: 30px;
      line-height: 1;
      font-weight: 700;
      position: absolute;
      opacity: 0.7;
    }

    &::after {
      content: "❞";
      font-size: 30px;
      position: absolute;
      right: 8px;
      bottom: 0;
      color: $primaryColor;
      opacity: 0.7;
    }

    p {
      color: #595959;
      line-height: 2;
    }
  }

  ol,
  ul {
    color: #595959;
    padding-left: 28px;

    li {
      margin-bottom: 0;
      list-style: inherit;

      & .task-list-item {
        list-style: none;

        ul,
        ol {
          margin-top: 0;
        }
      }
    }

    ul,
    ol {
      margin-top: 3px;
    }
  }

  ol li {
    padding-left: 6px;
  }

  @media (max-width: 720px) {
    h1 {
      font-size: 24px;
    }
    h2 {
      font-size: 20px;
    }
    h3 {
      font-size: 18px;
    }
  }
}
</style>
